package com.bubbles.workspace.sync.util;

import cn.hutool.v7.extra.spring.SpringUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.bubbles.workspace.sync.entity.source.metadata.SourceCatalog;
import com.bubbles.workspace.sync.entity.source.metadata.SourceJdbc;
import com.bubbles.workspace.sync.properties.SshProperties;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>description:  </p>
 *
 * @author CryptoNeedle
 * @date 2025-07-24
 */
public class JdbcUtil {
    
    private static final Map<String, DruidDataSource> DATA_SOURCE_MAP = new ConcurrentHashMap<>();
    private static final Map<String, JdbcTemplate> JDBC_TEMPLATE_MAP = new ConcurrentHashMap<>();
    
    public static DruidDataSource initDataSource(SourceCatalog sourceCatalog) {
        SourceJdbc sourceJdbc = sourceCatalog.getSourceJdbc();
        if (sourceJdbc == null) {
            throw new RuntimeException("[JdbcTemplateUtil] SourceJdbc is null => SourceCatalog=" + sourceCatalog);
        }
        return initDataSource(sourceJdbc);
    }
    
    public static DruidDataSource initDataSource(SourceJdbc sourceJdbc) {
        if (sourceJdbc == null) {
            throw new RuntimeException("[JdbcTemplateUtil] SourceJdbc is null");
        }
        
        SshProperties sshProperties = SpringUtil.getBean(SshProperties.class);
        if (sshProperties == null) {
            throw new RuntimeException("[JdbcTemplateUtil] SshProperties is null");
        }
        
        SshManager sshManager = SpringUtil.getBean(SshManager.class);
        if (sshManager == null) {
            throw new RuntimeException("[JdbcTemplateUtil] SshManager is null");
        }
        
        return DATA_SOURCE_MAP.computeIfAbsent(sourceJdbc.getStringKey(), key -> {
            DruidDataSource dataSource = new DruidDataSource();
            // 基本配置
            dataSource.setDriverClassName(sourceJdbc.getDriverClass());
            dataSource.setUrl(sourceJdbc.getDataSourceUrl(sshProperties, sshManager));
            dataSource.setUsername(sourceJdbc.getUserName());
            dataSource.setPassword(sourceJdbc.getPassword());
            dataSource.setConnectProperties(sourceJdbc.getDatabaseType().getDatabase().getProperties());
            // 连接池配置
            dataSource.setInitialSize(1);
            dataSource.setMinIdle(1);
            dataSource.setMaxActive(1);
            dataSource.setMaxActive(60000);
            // 连接有效性检测配置
            dataSource.setValidationQuery(sourceJdbc.getDatabaseType().getDatabase().getDatabaseValidSql());
            dataSource.setTestWhileIdle(true);
            dataSource.setTestOnBorrow(true);
            dataSource.setTestOnReturn(false);
            dataSource.setTimeBetweenEvictionRunsMillis(60000);
            // 其他配置
            dataSource.setPoolPreparedStatements(true);
            return dataSource;
        });
    }
    
    public static JdbcTemplate getJdbcTemplate(SourceCatalog sourceCatalog) {
        return getJdbcTemplate(sourceCatalog.getSourceJdbc());
    }
    
    public static JdbcTemplate getJdbcTemplate(SourceJdbc sourceJdbc) {
        String key = sourceJdbc.getStringKey();
        return JDBC_TEMPLATE_MAP.computeIfAbsent(key, k -> new JdbcTemplate(initDataSource(sourceJdbc)));
    }
}